package com.rpinfo.editalog.config;

import com.rpinfo.editalog.util.Util;
import java.awt.event.KeyEvent;

/**
 *
 * @author camilo
 */
public class CfgJDialog extends javax.swing.JDialog {

  private static CfgJDialog dialog;

  public static CfgJDialog getInstance(java.awt.Frame parent, boolean modal) {
    if (dialog == null) {
      dialog = new CfgJDialog(parent, modal);
    }
    return dialog;
  }

  /**
   * Creates new form Config
   */
  private CfgJDialog(java.awt.Frame parent, boolean modal) {
    super(parent, modal);
    initComponents();
    getCfg();
    jtfIp.requestFocus();
  }

  /**
   * This method is called from within the constructor to initialize the form. WARNING: Do NOT
   * modify this code. The content of this method is always regenerated by the Form Editor.
   */
  @SuppressWarnings("unchecked")
  // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
  private void initComponents() {

    jtpCfg = new javax.swing.JTabbedPane();
    jpBancoDados = new javax.swing.JPanel();
    jLabel2 = new javax.swing.JLabel();
    jtfIp = new javax.swing.JTextField();
    jLabel3 = new javax.swing.JLabel();
    jtfPorta = new javax.swing.JTextField();
    jLabel4 = new javax.swing.JLabel();
    jtfNomeBd = new javax.swing.JTextField();
    jLabel6 = new javax.swing.JLabel();
    jtfUser = new javax.swing.JTextField();
    jLabel7 = new javax.swing.JLabel();
    jpfPass = new javax.swing.JPasswordField();
    jPanel1 = new javax.swing.JPanel();
    jLabel1 = new javax.swing.JLabel();
    jtfCaminhoLog = new javax.swing.JTextField();
    jPanel2 = new javax.swing.JPanel();
    jbGravar = new javax.swing.JButton();
    jbCancelar = new javax.swing.JButton();

    setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
    setTitle("Configurações");
    setModal(true);
    setResizable(false);

    jtpCfg.setBorder(javax.swing.BorderFactory.createEtchedBorder());

    jLabel2.setText("IP");

    jtfIp.addKeyListener(new java.awt.event.KeyAdapter() {
      public void keyPressed(java.awt.event.KeyEvent evt) {
        jtfIpKeyPressed(evt);
      }
    });

    jLabel3.setText("Porta");

    jtfPorta.addKeyListener(new java.awt.event.KeyAdapter() {
      public void keyPressed(java.awt.event.KeyEvent evt) {
        jtfPortaKeyPressed(evt);
      }
    });

    jLabel4.setText("Nome do BD");

    jtfNomeBd.addKeyListener(new java.awt.event.KeyAdapter() {
      public void keyPressed(java.awt.event.KeyEvent evt) {
        jtfNomeBdKeyPressed(evt);
      }
    });

    jLabel6.setText("Usuário");

    jtfUser.addKeyListener(new java.awt.event.KeyAdapter() {
      public void keyPressed(java.awt.event.KeyEvent evt) {
        jtfUserKeyPressed(evt);
      }
    });

    jLabel7.setText("Senha");

    jpfPass.addKeyListener(new java.awt.event.KeyAdapter() {
      public void keyPressed(java.awt.event.KeyEvent evt) {
        jpfPassKeyPressed(evt);
      }
    });

    javax.swing.GroupLayout jpBancoDadosLayout = new javax.swing.GroupLayout(jpBancoDados);
    jpBancoDados.setLayout(jpBancoDadosLayout);
    jpBancoDadosLayout.setHorizontalGroup(
      jpBancoDadosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(jpBancoDadosLayout.createSequentialGroup()
        .addContainerGap()
        .addGroup(jpBancoDadosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
          .addGroup(jpBancoDadosLayout.createSequentialGroup()
            .addComponent(jLabel2)
            .addGap(110, 110, 110)
            .addComponent(jLabel3)
            .addGap(29, 29, 29)
            .addComponent(jLabel4))
          .addGroup(jpBancoDadosLayout.createSequentialGroup()
            .addComponent(jtfIp, javax.swing.GroupLayout.PREFERRED_SIZE, 116, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addComponent(jtfPorta, javax.swing.GroupLayout.PREFERRED_SIZE, 62, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addComponent(jtfNomeBd, javax.swing.GroupLayout.PREFERRED_SIZE, 150, javax.swing.GroupLayout.PREFERRED_SIZE))
          .addGroup(jpBancoDadosLayout.createSequentialGroup()
            .addGroup(jpBancoDadosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
              .addGroup(jpBancoDadosLayout.createSequentialGroup()
                .addComponent(jtfUser, javax.swing.GroupLayout.PREFERRED_SIZE, 178, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED))
              .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jpBancoDadosLayout.createSequentialGroup()
                .addComponent(jLabel6)
                .addGap(130, 130, 130)))
            .addGroup(jpBancoDadosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
              .addComponent(jLabel7)
              .addComponent(jpfPass, javax.swing.GroupLayout.PREFERRED_SIZE, 156, javax.swing.GroupLayout.PREFERRED_SIZE))))
        .addContainerGap(90, Short.MAX_VALUE))
    );
    jpBancoDadosLayout.setVerticalGroup(
      jpBancoDadosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(jpBancoDadosLayout.createSequentialGroup()
        .addContainerGap()
        .addGroup(jpBancoDadosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
          .addComponent(jLabel2)
          .addComponent(jLabel3)
          .addComponent(jLabel4))
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
        .addGroup(jpBancoDadosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
          .addComponent(jtfIp, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
          .addComponent(jtfPorta, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
          .addComponent(jtfNomeBd, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
        .addGroup(jpBancoDadosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
          .addComponent(jLabel6)
          .addComponent(jLabel7))
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
        .addGroup(jpBancoDadosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
          .addComponent(jtfUser, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
          .addComponent(jpfPass, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
    );

    jtpCfg.addTab("Banco de dados", jpBancoDados);

    jLabel1.setText("Caminho para arquivo de log:");

    javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
    jPanel1.setLayout(jPanel1Layout);
    jPanel1Layout.setHorizontalGroup(
      jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(jPanel1Layout.createSequentialGroup()
        .addContainerGap()
        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
          .addComponent(jtfCaminhoLog)
          .addGroup(jPanel1Layout.createSequentialGroup()
            .addComponent(jLabel1)
            .addGap(0, 211, Short.MAX_VALUE)))
        .addContainerGap())
    );
    jPanel1Layout.setVerticalGroup(
      jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(jPanel1Layout.createSequentialGroup()
        .addContainerGap()
        .addComponent(jLabel1)
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
        .addComponent(jtfCaminhoLog, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        .addContainerGap(62, Short.MAX_VALUE))
    );

    jtpCfg.addTab("Log", jPanel1);

    getContentPane().add(jtpCfg, java.awt.BorderLayout.CENTER);

    jbGravar.setText("Gravar");
    jbGravar.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        jbGravarActionPerformed(evt);
      }
    });

    jbCancelar.setText("Cancelar");
    jbCancelar.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        jbCancelarActionPerformed(evt);
      }
    });

    javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
    jPanel2.setLayout(jPanel2Layout);
    jPanel2Layout.setHorizontalGroup(
      jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(jPanel2Layout.createSequentialGroup()
        .addContainerGap()
        .addComponent(jbGravar)
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
        .addComponent(jbCancelar)
        .addContainerGap(284, Short.MAX_VALUE))
    );

    jPanel2Layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {jbCancelar, jbGravar});

    jPanel2Layout.setVerticalGroup(
      jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(jPanel2Layout.createSequentialGroup()
        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
          .addComponent(jbGravar)
          .addComponent(jbCancelar))
        .addGap(0, 12, Short.MAX_VALUE))
    );

    getContentPane().add(jPanel2, java.awt.BorderLayout.SOUTH);

    pack();
    setLocationRelativeTo(null);
  }// </editor-fold>//GEN-END:initComponents

  private void jbCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbCancelarActionPerformed
    dispose();
  }//GEN-LAST:event_jbCancelarActionPerformed

  private void jbGravarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbGravarActionPerformed
    this.setCfg();
    this.dispose();
  }//GEN-LAST:event_jbGravarActionPerformed

  private void jtfIpKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jtfIpKeyPressed
    if (evt.getKeyCode() == KeyEvent.VK_ENTER
            || evt.getKeyCode() == KeyEvent.VK_DOWN) {
      jtfPorta.requestFocus();
    }
  }//GEN-LAST:event_jtfIpKeyPressed

  private void jtfPortaKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jtfPortaKeyPressed
    if (evt.getKeyCode() == KeyEvent.VK_ENTER
            || evt.getKeyCode() == KeyEvent.VK_DOWN) {
      jtfNomeBd.requestFocus();
    }
  }//GEN-LAST:event_jtfPortaKeyPressed

  private void jtfNomeBdKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jtfNomeBdKeyPressed
    if (evt.getKeyCode() == KeyEvent.VK_ENTER
            || evt.getKeyCode() == KeyEvent.VK_DOWN) {
      jtfUser.requestFocus();
    }
  }//GEN-LAST:event_jtfNomeBdKeyPressed

  private void jtfUserKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jtfUserKeyPressed
    if (evt.getKeyCode() == KeyEvent.VK_ENTER
            || evt.getKeyCode() == KeyEvent.VK_DOWN) {
      jpfPass.requestFocus();
    }
  }//GEN-LAST:event_jtfUserKeyPressed

  private void jpfPassKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jpfPassKeyPressed
    if (evt.getKeyCode() == KeyEvent.VK_ENTER
            || evt.getKeyCode() == KeyEvent.VK_DOWN) {
      jbGravar.requestFocus();
    }
  }//GEN-LAST:event_jpfPassKeyPressed

  /**
   * @param args the command line arguments
   */
  public static void main(String args[]) {
    /* Set the Nimbus look and feel */
    //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
     * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
     */
    try {
      for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
        if ("Nimbus".equals(info.getName())) {
          javax.swing.UIManager.setLookAndFeel(info.getClassName());
          break;
        }
      }
    } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | javax.swing.UnsupportedLookAndFeelException ex) {
      java.util.logging.Logger.getLogger(CfgJDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    }
    //</editor-fold>

    /* Create and display the dialog */
    java.awt.EventQueue.invokeLater(new Runnable() {
      @Override
      public void run() {
        CfgJDialog dialog = new CfgJDialog(new javax.swing.JFrame(), true);
        dialog.addWindowListener(new java.awt.event.WindowAdapter() {
          @Override
          public void windowClosing(java.awt.event.WindowEvent e) {
            System.exit(0);
          }
        });
        dialog.setVisible(true);
      }
    });
  }
  // Variables declaration - do not modify//GEN-BEGIN:variables
  private javax.swing.JLabel jLabel1;
  private javax.swing.JLabel jLabel2;
  private javax.swing.JLabel jLabel3;
  private javax.swing.JLabel jLabel4;
  private javax.swing.JLabel jLabel6;
  private javax.swing.JLabel jLabel7;
  private javax.swing.JPanel jPanel1;
  private javax.swing.JPanel jPanel2;
  private javax.swing.JButton jbCancelar;
  private javax.swing.JButton jbGravar;
  private javax.swing.JPanel jpBancoDados;
  private javax.swing.JPasswordField jpfPass;
  private javax.swing.JTextField jtfCaminhoLog;
  private javax.swing.JTextField jtfIp;
  private javax.swing.JTextField jtfNomeBd;
  private javax.swing.JTextField jtfPorta;
  private javax.swing.JTextField jtfUser;
  private javax.swing.JTabbedPane jtpCfg;
  // End of variables declaration//GEN-END:variables

  private void getCfg() {
    CfgPropUtil cfgProp = new CfgPropUtil();
    Cfg cfg = cfgProp.getCfgProp();

    jtfIp.setText(cfg.getIp());
    jtfPorta.setText(cfg.getPorta());
    jtfNomeBd.setText(cfg.getNomeBd());
    jtfUser.setText(cfg.getUser());
    jpfPass.setText(cfg.getPassword());
    jtfCaminhoLog.setText(cfg.getCaminhoLog());
  }

  private void setCfg() {
    CfgPropUtil cfgProp = new CfgPropUtil();
    Cfg cfg = new Cfg();

    cfg.setIp(jtfIp.getText());
    cfg.setPorta(jtfPorta.getText());
    cfg.setNomeBd(jtfNomeBd.getText());
    cfg.setUser(jtfUser.getText());
    cfg.setPassword(Util.getPasswordJpf(jpfPass));
    cfg.setCaminhoLog(jtfCaminhoLog.getText());

    cfgProp.setCfgProp(cfg);
  }
}
